﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace DotNETX.Utility.Config
{
    public class XmlSerializeable<T>
        where T : class
    {
        public string ToXmlString()
        {
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Encoding = Encoding.UTF8;
            return ToXmlString(setting);
        }

        public string ToXmlString(XmlWriterSettings settings)
        {
            XmlSerializerNamespaces xmlNamespaces = new XmlSerializerNamespaces();
            return ToXmlString(settings, xmlNamespaces);
        }

        public string ToXmlString(XmlWriterSettings settings, XmlSerializerNamespaces xmlNamespaces)
        {
            string xml = null;
            using (System.IO.Stream memStream = new System.IO.MemoryStream())
            {
                using (XmlWriter writer = XmlWriter.Create(memStream, settings))
                {
                    XmlSerializer serializer = new XmlSerializer(this.GetType());
                    serializer.Serialize(writer, this, xmlNamespaces);
                    writer.Flush();
                    writer.Close();
                    memStream.Position = 0;
                    using (StreamReader reader = new StreamReader(memStream))
                    {
                        xml = reader.ReadToEnd();
                    }
                }
            }
            return xml;
        }

        public bool ToXmlFile(string file)
        {
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Encoding = Encoding.UTF8;
            return ToXmlFile(file, setting);
        }

        public bool ToXmlFile(string file, XmlWriterSettings settings)
        {
            XmlSerializerNamespaces xmlNamespaces = new XmlSerializerNamespaces();
            return ToXmlFile(file, settings, xmlNamespaces);
        }

        public bool ToXmlFile(string file, XmlWriterSettings settings, XmlSerializerNamespaces xmlNamespaces)
        {
            try
            {
                if (System.IO.File.Exists(file)) System.IO.File.Delete(file);
                using (FileStream fstream = new FileStream(file, FileMode.Create))
                {
                    using (XmlWriter writer = XmlWriter.Create(fstream, settings))
                    {
                        XmlSerializer serializer = new XmlSerializer(this.GetType());
                        serializer.Serialize(writer, this, xmlNamespaces);
                        writer.Flush();
                        writer.Close();
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static T ParseFromFile(string file)
        {
            try
            {
                using (FileStream fs = new FileStream(file, FileMode.Open))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(T));
                    object obj = ser.Deserialize(fs);
                    return obj as T;
                }
            }
            catch
            {
                return null;
            }
        }

        public static T Parse(string xml)
        {
            XmlReader reader = XmlReader.Create(new StringReader(xml));
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(reader);

        }
    }
}
